package com.maslyk.invitation.audit;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.maslyk.invitation.BeanService;
import com.maslyk.invitation.entitybean.Auditlog;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;
import com.maslyk.invitation.to.CurrentUserTO;

public class AuditLogUtil {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void LogIt(OperationType action, Object entity, String changes) {

		if (!(entity instanceof GenericEntity))
			return;

		GenericEntity genericEntity = (GenericEntity) entity;

		CurrentUserTO currentUser = (CurrentUserTO) BeanService.getSessionScopeBean("currentUserTO");
		Auditlog auditRecord = new Auditlog(currentUser.getCurrentSalesman(),
				action.name(), changes, new Date(), genericEntity.getId(),
				entity.getClass().getAnnotation(ObjectName.class).label(),
				genericEntity.getKey());
		entityManager.persist(auditRecord);

	}
}
